package dao.impl;

        import dao.UserDao;
        import db.DBConstant;
        import db.DBUtil;
        import entity.User;

        import java.sql.*;

public class UserDaoImpl implements UserDao {

    @Override
    public User findOne(String name, String password) throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        User user = null;
        //String sql = "select * from "+DBConstant.TB_USER+" where "+ DBConstant.USER_NAME +"=? and "+DBConstant.USER_PASSWORD+"=?";
        String sql = "select * from user where username=? and password=?";
        try {
            conn = DBUtil.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setString(1,name);
            ps.setString(2,password);
            rs = ps.executeQuery();
            if(rs.next()){
                user = new User();
                user.set_id(rs.getInt(1));
                user.setUsername(rs.getString(2));
                user.setPassword(rs.getString(3));
            }
        }catch (SQLException e){
            e.printStackTrace();
            throw new SQLException("查询数据失败");
        }finally {
            DBUtil.close(rs,ps,conn);
        }
        return user;
    }

}
